package com.zhijian.warehouse.manage.job;


import com.gg.reader.api.dal.GClient;
import com.zhijian.util.BeanUtil;
import com.zhijian.warehouse.manage.config.access.AccessConfig;
import com.zhijian.warehouse.manage.entity.vo.request.AccessAioAndroidBingSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.AccessDoorDeploySearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.SseMessageSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.AccessAioAndroidBingVo;
import com.zhijian.warehouse.manage.entity.vo.response.AccessDoorDeployVo;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.service.AccessAioAndroidBingService;
import com.zhijian.warehouse.manage.service.AccessNotifyService;
import com.zhijian.warehouse.manage.service.AccessOptService;
import com.zhijian.warehouse.manage.service.DictionaryService;
import com.zhijian.warehouse.manage.util.access.AccessUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 应用启动后初始化
 *
 * @Author chengqing
 * @Version 1.0.1
 */
@Slf4j
@Component
public class InitRunner implements ApplicationRunner {
    @Autowired
    private AccessConfig accessConfig;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private AccessOptService accessOptService;
    @Autowired
    private AccessAioAndroidBingService accessAioAndroidBingService;

    @Autowired
    private AccessNotifyService accessNotifyService;

    /**
     * {@inheritDoc}
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //初始化字典
        //dictionaryService.restoreCache();

        //一体机
        List<AccessAioAndroidBingVo> accessAioAndroidBingVos = accessAioAndroidBingService.selectList(AccessAioAndroidBingSearchVo.builder().build());
        if (BeanUtil.isNotEmpty(accessAioAndroidBingVos))
            accessAioAndroidBingVos.forEach(accessAioAndroidBingVo -> { //一体机参数
                accessNotifyService.initReadEpc(SseMessageSearchVo.builder().build(), new GClient(), accessAioAndroidBingVo);
            });
        //通道门
        List<AccessDoorDeployVo> accessDoorDeployVos = accessOptService.selectDoorList(AccessDoorDeploySearchVo.builder().isDelete(DelFlagEnum.USING.value).build());
        if (BeanUtil.isNotEmpty(accessDoorDeployVos))
            accessDoorDeployVos.forEach(accessDoorDeployVo -> { //通道门参数
                AccessUtil.initAccess(new GClient(), accessConfig, accessOptService, accessDoorDeployVo);
            });
    }
}
